import requests
import pyttsx3




"""
# 百度语言翻译 同时报语音
"""
def baidu():
    while True:
        content = input("请输入翻译内容（英文）：\n")
        url = 'http://fanyi.baidu.com/v2transapi/'
        data = {
            ' from': 'en', 'to': 'zh', 'query': content,
            'transtype': 'translang',
            'simple_means_flag': '3',
        }

        header = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36 SE 2.X MetaSr 1.0'}
        response = requests.post(url, data, header)
        head = response.headers
        resout = response.json()['trans_result']['data'][0]['dst']
        print(resout)

        speaker = pyttsx3.init()
        speaker.say(content)
        speaker.runAndWait()



def yuYin(parm):
    url = "http://fanyi.baidu.com/gettts?lan=en&text=" + parm + "&spd=3&source=web"
    header = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36 SE 2.X MetaSr 1.0'}
    mp3 = requests.post(url, header)
    print(mp3)
    return mp3



# 朗读
def requetAloud(str):
    speaker = pyttsx3.init()
    speaker.say(str)
    speaker.runAndWait()


if __name__ == "__main__":
    baidu()
